package app.fugema.shop.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.base.common.model.bean.PayResponse;
import com.jeremyliao.liveeventbus.LiveEventBus;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.helper.WeChatHelper;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;


/**
 * @author sugar
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WeChatHelper.init(this);
        WeChatHelper.handResp(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        WeChatHelper.handResp(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.e(TAG, "onPayFinish, errCode = " + resp.errCode);
        // 支付类
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            PayResponse payResp = new PayResponse(PayResponse.PayWay.WAY_WECHAT, PayResponse.PayStatus.STATUS_UNKOWN);
            if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
                // 微信支付成功
                payResp.setPayStatus(PayResponse.PayStatus.STATUS_SUCCESS);
            } else if (resp.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) {
                payResp.setPayStatus(PayResponse.PayStatus.STATUS_CANCEL);
            } else if (resp.errCode == BaseResp.ErrCode.ERR_SENT_FAILED) {
                payResp.setPayStatus(PayResponse.PayStatus.STATUS_FAIL);
            }
            LiveEventBus.get(AppConstants.PAY_NOTIFY).post(payResp);
            finish();
        }


    }
}